package com.xzj.admin.resource.analysis.handle.analysis.pg;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.xzj.admin.analysis.base.common.cache.WaitDelCache;
import com.xzj.admin.analysis.base.common.handle.CommonAbstractAnalysisHandle;
import com.xzj.admin.analysis.base.constants.CommonEnums;
import com.xzj.admin.analysis.base.domain.CommonFlowInfo;
import com.xzj.admin.analysis.base.domain.ResMessageManage;
import com.xzj.admin.resource.analysis.handle.analysis.cmdb.SourceToCmdbHandle;
import com.xzj.admin.resource.analysis.mapper.ResMessageManageMapper;
import com.xzj.common.plugins.disruptor.util.DisruptorUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
 * @program: xzj-admin
 * @ClassName: MsgAnalysisEndHandle
 * @description: 遇到结束标识处理器
 * @author: XiaZunJun
 * @create: 2024-11-19 11:01
 */
@Component
@Slf4j
public class MsgAnalysisEndHandle {
    @Resource
    private CommonAbstractAnalysisHandle commonAbstractAnalysisHandle;

    @Resource
    private ResMessageManageMapper resMessageManageMapper;

    @Resource
    private SourceToCmdbHandle sourceToCmdbHandle;

    private final DisruptorUtil<CommonFlowInfo> disruptorUtil =
            new DisruptorUtil<>("middleAfter", new Consumer<CommonFlowInfo>() {
                @Override
                public void accept(CommonFlowInfo info) {
                    sourceToCmdbHandle.foreachRmPoolTheme(info);
                }
            });

    public void analysisEndHandle(CommonFlowInfo info) {
        log.info("遇到结束标识处理器");
        Map<String, Map<String, String>> rmPoolMap = WaitDelCache.removeMd5Map(info.getRmPoolId());
        if (null != rmPoolMap) {
            for (String sourceTheme : rmPoolMap.keySet()) {
                Map<String, String> delCache = rmPoolMap.get(sourceTheme);
                judgeDelTo(info, sourceTheme, delCache);
            }
        } else {
            log.info("当前资源池无任何资源需要删除！");
        }
        disruptorUtil.sendMessage(info);
    }

    /**
     * 资源结束标识调用删除逻辑
     *
     * @param info        解析流转对象，主要公用信息的保存
     * @param sourceTheme 消息源主题
     * @param delCache    需要删除的缓存
     * @author xiazunjun
     * @date 2023/11/1 11:14
     */
    public void judgeDelTo(CommonFlowInfo info, String sourceTheme, Map<String, String> delCache) {
        List<ResMessageManage> delInfos = new ArrayList<>();
        String messageIds = "";
        for (String key : delCache.keySet()) {
            if (!key.equals(CommonEnums.CACHE_KEY.PREFIX.getValue())) {
                ResMessageManage middleMessage = buildMessageManage(info, sourceTheme, key);
                commonAbstractAnalysisHandle.analysisSpecial(middleMessage,info);
                delInfos.add(middleMessage);
                if(StrUtil.isNotEmpty(messageIds)){
                    messageIds +=",";
                }
                messageIds += key;
            }
        }
        log.info("{} - {}-当前主题{}删除数据条数为:{}", info.getRmPoolId(), info.getCloudType(), sourceTheme,
                delInfos.size());
        if (!delInfos.isEmpty()) {
            log.info("{} - {}-当前主题{}删除数据条数为:{}", info.getRmPoolId(), info.getCloudType(), sourceTheme,
                    JSON.toJSONString(delInfos));
            resMessageManageMapper.batchUpdateDel(messageIds,delInfos.get(0));
            // 修改对应任务的删除数
            // 1. 先获取源表数据信息
            // ResMessageSource messageSource = BaseCache.getMessageSource(sourceTheme);
        }
    }

    private static ResMessageManage buildMessageManage(CommonFlowInfo info, String sourceTheme, String key) {
        ResMessageManage middleMessage = new ResMessageManage();
        middleMessage.setMessageId(key);
        middleMessage.setMessageTag(CommonEnums.MESSAGE_TAG.del.name());
        middleMessage.setCloudId(info.getCloudId());
        middleMessage.setCloudType(info.getCloudType());
        middleMessage.setRmPoolId(info.getRmPoolId());
        middleMessage.setStandardCode(info.getStandardCode());
        middleMessage.setMessageSource(sourceTheme);
        middleMessage.setMessageTime(info.getMessageDate());
        return middleMessage;
    }
}
